<template>
  <div>
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList()"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="searchNetworkDayList"
          size="small"
          label-width="140px"
        >
          <el-form-item label="网点1名称:">
            <el-select
              v-model="searchNetworkDayList.networkId1"
              no-match-text="不存在该网点,请重新输入"
              clearable
              filterable
              :filter-method='getNetwork1'
              style="width: 203px"
              placeholder="请输入要搜索的网点名称"
            >
              <el-option
                v-for="(item, index) in networkOptions1"
                :key="index"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="网点2名称:">
            <el-select
              v-model="searchNetworkDayList.networkId2"
              no-match-text="不存在该网点,请重新输入"
              clearable
              filterable
              :filter-method='getNetwork2'
              style="width: 203px"
              placeholder="请输入要搜索的网点名称"
            >
              <el-option
                v-for="(item, index) in networkOptions2"
                :key="index"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="路线耗时:">
            <el-input
              style="width: 203px"
              v-model="searchNetworkDayList.day"
              placeholder="请输入要搜索的路线耗时"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card style="margin-top: 15px" class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button
        class="btn-add"
        style="float: right"
        @click="addDialogVisible = true"
        size="mini"
      >
        添加
      </el-button>

      <el-dialog
        title="网点路线添加"
        :visible.sync="addDialogVisible"
        width="60%"
        :before-close="handleClose"
      >
        <span>
          <el-card class="filter-container" shadow="never">
            <div style="margin-top: 15px">
              <el-form
                :rules="rules"
                ref="addNetworkDay"
                :inline="true"
                :model="addNetworkDay"
                size="small"
                label-width="140px"
              >
                <el-form-item prop="networkId1" label="网点1名称:">
                  <el-select
                    v-model="addNetworkDay.networkId1"
                    no-match-text="不存在该网点,请重新输入"
                    clearable
                    filterable
                    :filter-method='getNetwork3'
                    style="width: 203px"
                    placeholder="请输入要搜索的网点名称"
                  >
                    <el-option
                      v-for="(item, index) in networkOptions3"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item prop="networkId2" label="网点2名称:">
                  <el-select
                    v-model="addNetworkDay.networkId2"
                    no-match-text="不存在该网点,请重新输入"
                    clearable
                    filterable
                    :filter-method='getNetwork3_1'
                    style="width: 203px"
                    placeholder="请输入要搜索的网点名称"
                  >
                    <el-option
                      v-for="(item, index) in networkOptions3_1"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item prop="day" label="路线耗时:">
                  <el-input
                    style="width: 203px"
                    v-model="addNetworkDay.day"
                    placeholder="请输入要搜索的路线耗时"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-form>
            </div>
          </el-card>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleAdd()">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
    <div style="margin-top: 15px" class="table-container">
      <el-table
        :data="networkDayList"
        style="width: 100%"
        height="260px"
        v-loading="listLoading"
        border
      >
        <el-table-column label="网点路线id" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="网点1" width="350" align="center">
          <template slot-scope="scope">{{ scope.row.netWork1.name }}</template>
        </el-table-column>
        <el-table-column label="网点2" width="350" align="center">
          <template slot-scope="scope">{{ scope.row.netWork2.name }}</template>
        </el-table-column>
        <el-table-column label="路线耗时" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.day }}</template>
        </el-table-column>
        <el-table-column label="操作" width="350" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="
                (messageEditDialogVisible = true), showMessageData(scope.$index)
              "
              >修改
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.id)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        title="网点路线修改"
        :visible.sync="messageEditDialogVisible"
        width="60%"
        :before-close="handleClose"
      >
        <span>
          <el-card class="filter-container" shadow="never">
            <div style="margin-top: 15px">
              <el-form
                :inline="true"
                :model="editMessage"
                size="small"
                label-width="140px"
              >
                <el-form-item label="网点1名称:">
                  <el-select
                    v-model="editMessage.netWork1.name"
                    no-match-text="不存在该网点,请重新输入"
                    clearable
                    filterable
                    :filter-method='getNetwork1_1'
                    style="width: 203px"
                    placeholder="请输入要搜索的网点名称"
                  >
                    <el-option
                      v-for="(item, index) in networkOptions1_1"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="网点2名称:">
                  <el-select
                    v-model="editMessage.netWork2.name"
                    no-match-text="不存在该网点,请重新输入"
                    clearable
                    filterable
                    :filter-method='getNetwork2_1'
                    style="width: 203px"
                    placeholder="请输入要搜索的网点名称"
                  >
                    <el-option
                      v-for="(item, index) in networkOptions2_1"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="路线耗时:">
                  <el-input
                    style="width: 203px"
                    v-model="editMessage.day"
                    placeholder="请输入要搜索的路线耗时"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-form>
            </div>
          </el-card>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="messageEditDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditMessage()"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        layout="prev, pager, next"
        :page-count="pageCunt"
        :current-page.sync="currentPage"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  name: "networkDay",
  data() {
    return {
      addDialogVisible: false,
      messageEditDialogVisible: false,
      addNetworkDay: {},
      currentNetworkList: {
        day: "",
        networkId1: "",
        networkId2: "",
        pageNum: "1",
        pageSize: "5",
      },
      currentPage:1,
      searchNetworkDayList: {},
      listLoading: false,
      networkDayList: [],
      searchNetworkDayList: {
        day: "",
        networkId1: "",
        networkId2: "",
        pageNum: "1",
        pageSize: "5",
      },
      editMessage: {
        day: "",
        id: "",
        netWork1: {},
        netWork2: {},
      },
      networkOptions1: [],
      networkOptions2: [],
      networkOptions3: [],
      networkOptions3_1: [],
      networkOptions1_1: [],
      networkOptions2_1: [],
      rules: {
        networkId1: [
          { required: true, message: "请输入网点1名称", trigger: "blur" },
        ],
        networkId2: [
          { required: true, message: "请输入网点2名称", trigger: "blur" },
        ],
        day: [
          {
            required: true,
            message: "请输入两网点间的路线耗时",
            trigger: "blur",
          },
        ],
      },
      NetworkList: [],
      pageCunt: 1,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      this.axios
        .get("api/auth/sys/networkDay/list", {
          params: this.searchNetworkDayList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.networkDayList = response.data.value.data;
            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            this.currentPage = 1
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    handleAdd() {
      this.$refs.addNetworkDay.validate((valid) => {
        if (
          valid &&
          this.addNetworkDay.networkId1 != this.addNetworkDay.networkId2
        ) {
          this.$confirm("请再次确认是否添加")
            .then((_) => {
              this.axios
                .post(
                  "api/auth/sys/networkDay/add?day=" +
                    this.addNetworkDay.day +
                    "&networkId1=" +
                    this.addNetworkDay.networkId1 +
                    "&networkId2=" +
                    this.addNetworkDay.networkId2
                )
                .then((response) => {
                  if (response.data.code == "200") {
                    this.$message({
                      message: "添加成功",
                      type: "success",
                    });
                    this.getList();
                    this.addDialogVisible = false;
                  } else {
                    this.$message.error("添加失败");
                  }
                })
                .catch((error) => {
                  this.$message.error(response.data.message);
                });
            })
            .catch((err) => {
              this.$message.error(err);
            });
        } else {
          this.$message.error("请填写完将带*号的选项,并且所选的两网点不能一样");
        }
      });
    },
    showMessageData(index) {
      this.editMessage = { ...this.networkDayList[index] };
      this.editMessage.netWork1 = {...this.networkDayList[index].netWork1};
      this.editMessage.netWork2 = {...this.networkDayList[index].netWork2};
      console.log(this.editMessage);
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    handleSearchList() {
      this.listLoading = true;
      this.axios
        .get("api/auth/sys/networkDay/list", {
          params: this.searchNetworkDayList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.networkDayList = response.data.value.data;
            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            this.currentPage = 1
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    handleResetSearch() {
      this.searchNetworkDayList = {
        day: "",
        networkId1: "",
        networkId2: "",
        pageNum: "1",
        pageSize: "5",
      };
    },
    handleCurrentChange(val) {
      this.listLoading = true;
      this.currentNetworkList.pageNum = val;
      this.axios
        .get("api/auth/sys/networkDay/list", {
          params: this.currentNetworkList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.networkDayList = response.data.value.data;
            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    handleEditMessage() {
      if (
        this.editMessage.netWork1.id != "" &&
        this.editMessage.netWork2.id != "" &&
        this.editMessage.day != "" &&
        this.editMessage.netWork1.id != this.editMessage.netWork2.id
      ) {
        this.$confirm("请再次确认是否修改")
          .then((_) => {
            console.log(this.editMessage);
            this.axios
              .post(
                "api/auth/sys/networkDay/update?id=" +
                  this.editMessage.id +
                  "&day=" +
                  this.editMessage.day +
                  "&networkId1=" +
                  this.editMessage.netWork1.id +
                  "&networkId2=" +
                  this.editMessage.netWork2.id
              )
              .then((response) => {
                if (response.data.code == "200") {
                  this.$message({
                    message: "修改成功",
                    type: "success",
                  });
                  this.getList();
                  this.messageEditDialogVisible = false;
                } else {
                  this.$message.error("修改失败");
                }
              })
              .catch((error) => {
                this.$message.error(response.data.message);
              });
          })
          .catch((err) => {
            this.$message.error(err);
          });
      } else {
        this.$message.error("请填写完所有的选项,并且所选的两网点不能一样");
      }
    },
    handleDelete(id) {
      this.$confirm("此操作将永久删除该网点路线, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios
            .post("api/auth/sys/networkDay/delete?id=" + id)
            .then((response) => {
              if (response.data.code == "200") {
                this.$message({
                  message: "删除成功",
                  type: "success",
                });
                this.getList();
              } else {
                this.$message.error("删除失败");
              }
            })
            .catch((error) => {
              this.$message.error(response.data.message);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    getNetwork1(val) {
      if (val != "") {
        this.axios
          .get(
            "api/sys/network/search?name="+val
          )
          .then((response) => {
            console.log(response)
            if (response.data.code == "200") {
              this.networkOptions1 = response.data.value
            } else {
              this.$message.error("未获取到网点数据");
            }
          })
          .catch((error) => {
            this.$message.error(response.data.message);
          });
      }else{
        this.networkOptions1 = []
      }
    },
    getNetwork2(val) {
      if (val != "") {
        this.axios
          .get(
            "api/sys/network/search?name="+val
          )
          .then((response) => {
            console.log(response)
            if (response.data.code == "200") {
              this.networkOptions2 = response.data.value
            } else {
              this.$message.error("未获取到网点数据");
            }
          })
          .catch((error) => {
            this.$message.error(response.data.message);
          });
      }else{
        this.networkOptions2 = []
      }
    },
    getNetwork3(val) {
      if (val != "") {
        this.axios
          .get(
            "api/sys/network/search?name="+val
          )
          .then((response) => {
            console.log(response)
            if (response.data.code == "200") {
              this.networkOptions3 = response.data.value
            } else {
              this.$message.error("未获取到网点数据");
            }
          })
          .catch((error) => {
            this.$message.error(response.data.message);
          });
      }else{
        this.networkOptions3 = []
      }
    },
    getNetwork3_1(val) {
      if (val != "") {
        this.axios
          .get(
            "api/sys/network/search?name="+val
          )
          .then((response) => {
            if (response.data.code == "200") {
              this.networkOptions3_1 = response.data.value
            console.log(this.networkOptions3_1)
            } else {
              this.$message.error("未获取到网点数据");
            }
          })
          .catch((error) => {
            this.$message.error(response.data.message);
          });
      }else{
        this.networkOptions3_1 = []
      }
    },
    getNetwork1_1(val) {
      if (val != "") {
        this.axios
          .get(
            "api/sys/network/search?name="+val
          )
          .then((response) => {
            console.log(response)
            if (response.data.code == "200") {
              this.networkOptions1_1 = response.data.value
            } else {
              this.$message.error("未获取到网点数据");
            }
          })
          .catch((error) => {
            this.$message.error(response.data.message);
          });
      }else{
        this.networkOptions1_1 = []
      }
    },
    getNetwork2_1(val) {
      if (val != "") {
        this.axios
          .get(
            "api/sys/network/search?name="+val
          )
          .then((response) => {
            console.log(response)
            if (response.data.code == "200") {
              this.networkOptions2_1 = response.data.value
            } else {
              this.$message.error("未获取到网点数据");
            }
          })
          .catch((error) => {
            this.$message.error(response.data.message);
          });
      }else{
        this.networkOptions2_1 = []
      }
    },
  },
};
</script>
